package com.mingtu.jgspatrol.activity;

import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.AdaptScreenUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.mingtu.common.activity.LoginActivity;
import com.mingtu.common.api.AppConfig;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.common.manager.ScreenManager;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.SPTools;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.jgspatrol.R;

import java.util.Timer;
import java.util.TimerTask;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.ups.JPushUPSManager;


public class WelcomeActivity extends AppCompatActivity {
    protected static final long ANIMATION_DELAY = 1000;
    private FrameLayout layoutWelcome;
    private ImageView ivBg;
    protected boolean isStatusBar = false;//是否沉浸状态栏
    protected boolean isFullScreen = false;//是否允许全屏
    protected boolean isScreenPortrait = true;//是否禁止旋转屏幕

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initScreenAdaption();
        setContentView(R.layout.activity_welcome);
        layoutWelcome = findViewById(R.id.layout_welcome);
        ivBg = findViewById(R.id.iv_bg);
        // 添加到Activity工具类
        ActivityUtil.getInstance().addActivity(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        // Activity销毁时，提示系统回收
        // System.gc();
        ActivityUtil.getInstance().removeActivity(this);     // 移除Activity
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Resources resources = this.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = 1;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());//字体的大小
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 点击手机上的返回键，返回上一层
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 移除Activity
            ActivityUtil.getInstance().removeActivity(this);
        }
        return super.onKeyDown(keyCode, event);
    }

    protected void initView() {
        // 隐藏标题栏
        if (getSupportActionBar() != null) getSupportActionBar().hide();
        ScreenManager screenManager = ScreenManager.getInstance();
        screenManager.setStatusBar(isStatusBar, this);
        screenManager.setScreenRoate(isScreenPortrait, this);
        screenManager.setFullScreen(isFullScreen, this);


        //沉侵式状态栏
        ImmersionBar immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarDarkFont(true);
        immersionBar.init();

//        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
//        alphaAnimation.setDuration(5000);
//        layoutWelcome.startAnimation(alphaAnimation);
//        alphaAnimation.setAnimationListener(new AnimationListener());


//        SVAnimationDrawable animation = new SVAnimationDrawable();
//        animation.setOnFrameAnimationListener(new SVAnimationDrawable.OnFrameAnimationListener() {
//            @Override
//            public void onStart() {
//                //动画开始
//            }
//
//            @Override
//            public void onEnd() {
//                //动画结束
//                String token = SPStaticUtils.getString(SPTools.token);
//                boolean isGreen = SPStaticUtils.getBoolean(SPTools.isAgree);
//                boolean isPublish = Constant.getInstance().IS_PUBLISH;
//
//                if (isPublish && !isGreen) {
//                    IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, AgreementActivity.class);
//                    return;
//                }
//
//                if (!StringUtils.isEmpty(token)) {
//                    IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, MainActivity.class);
//                } else {
//                    JPushInterface.deleteAlias(getApplicationContext(), Constant.TYPE_BIND_ALIAS);
//                    JPushUPSManager.turnOffPush(getApplicationContext(), null);
//                    IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, LoginActivity.class);
//                }
//            }
//        });

        AnimationDrawable animation = new AnimationDrawable();
        ApplicationInfo appInfo = getApplicationInfo();
        for (int i = 0; i <= 75; i++) {
            int resID = getResources().getIdentifier("icon_welcome_frame" + i, "drawable", appInfo.packageName);
            animation.addFrame(getResources().getDrawable(resID), 32);
        }
        animation.setOneShot(true);
        ivBg.setImageDrawable(animation);
        //开始动画
        animation.start();
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                String token = SPStaticUtils.getString(SPTools.token);
                boolean isGreen = SPStaticUtils.getBoolean(SPTools.isAgree);
                boolean isPublish = Constant.getInstance().IS_PUBLISH;

                if (isPublish && !isGreen) {
                    IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, AgreementActivity.class);
                    return;
                }

                if (!StringUtils.isEmpty(token)) {
                    IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, MainActivity.class);
                } else {
                    JPushInterface.deleteAlias(getApplicationContext(), Constant.TYPE_BIND_ALIAS);
                    JPushUPSManager.turnOffPush(getApplicationContext(), null);
                    IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, LoginActivity.class);
                }
            }
        }, 2432);


    }


    /**
     * 初始化屏幕适配
     */
    protected void initScreenAdaption() {
        if (ScreenUtils.isPortrait()) {
            if (ScreenUtils.isTablet()) {
                AdaptScreenUtils.adaptWidth(BaseApplication.getBaseApplication().getResources(), AppConfig.widthInPxTable);
            } else {
                ScreenUtils.adaptScreen4VerticalSlide(this, AppConfig.widthInPx);
            }

        } else {
            if (ScreenUtils.isTablet()) {
                AdaptScreenUtils.adaptHeight(BaseApplication.getBaseApplication().getResources(), AppConfig.heightInPxTable);
            } else {
                ScreenUtils.adaptScreen4HorizontalSlide(this, AppConfig.heightInPx);
            }
        }
    }

}
